package demo1;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

class MyStack {
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    public MyStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }
    //入栈
    public void push(int value) {
        //放入不为空的队列之中，若二者均为空，优先放入第一个队列中
        if(!queue1.isEmpty()) {
            queue1.offer(value);
        } else if(!queue2.isEmpty()) {
            queue2.offer(value);
        } else {
            queue1.offer(value);
        }
    }
    //出栈
    public int pop() {
        if(!queue1.isEmpty()) {
            int usedSize = queue1.size();
            for (int i = 0; i < usedSize-1; i++) {
                queue2.offer(queue1.poll());
            }
            return queue1.poll();
        } else if(!queue2.isEmpty()) {
            int usedSize = queue2.size();
            for (int i = 0; i < usedSize-1; i++) {
                queue1.offer(queue2.poll());
            }
            return queue2.poll();
        } else {
            return -1;
        }
    }
    //查看栈顶元素
    public int top() {
        if(!queue1.isEmpty()) {
            int usedSize = queue1.size();
            int x = -1;
            for (int i = 0; i < usedSize; i++) {
                x = queue1.poll();
                queue2.offer(x);
            }
            return x;
        } else if(!queue2.isEmpty()) {
            int usedSize = queue2.size();
            int x = -1;
            for (int i = 0; i < usedSize; i++) {
                x = queue2.poll();
                queue1.offer(x);
            }
            return x;
        } else {
            return -1;
        }
    }
}
